package com.project.util;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/9/7 17:10
 */
@Configuration
public class SysConfig {

    @Autowired
    CachingConnectionFactory connectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    //死信队列
    @Bean
    public Queue imgQueue(){
        return new Queue("imgQueue");
    }

//    @Bean
//    public Queue meituanQueue(){
//        //关联死信交换机
//        Map<String, Object> args = new HashMap<>(3);
//        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", "directDeadExchange");
//        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key","deadKEY");
//        // x-message-ttl  声明队列的TTL
//        args.put("x-message-ttl",5000);
////        return new Queue("meituanQueue");
//        return QueueBuilder.durable("meituanQueue").withArguments(args).build();
//    }

//    //死信交换机
//    @Bean
//    public DirectExchange directDeadExchange(){
//        return new DirectExchange("directDeadExchange");
//    }

    @Bean
    public DirectExchange imgDirect(){
        return new DirectExchange("imgDirect");
    }

//    @Bean
//    public Binding queueDeadTodirectDeadExchange(Queue queueDead,DirectExchange directDeadExchange){
//        return BindingBuilder.bind(queueDead).to(directDeadExchange).with("deadKEY");
//    }

    @Bean
    public Binding queueOneTodirectExchange(Queue imgQueue, DirectExchange imgDirect){
        return BindingBuilder.bind(imgQueue).to(imgDirect).with("img.Direct");
    }
}
